<template>
  <div class="user-tel">
    <!-- 验证旧手机号 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"  :disabled="true"></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="code">
       <el-input v-model="ruleForm.code" placeholder="请输入验证码">
       			  <template slot="append"><el-button style="height: 40px"
         :disabled="btnIsDisabled"
         class="btn-code"
         type="primary"
         @click="getVCode()"
       >{{VCodeText}}</el-button></template>
       </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="verifyPhone('ruleForm')">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "verifyPhone",
  data() {
    return {
      ruleForm: {},
      rules: {
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      },
      VCodeText: "获取验证码",
      btnIsDisabled: false,
    };
  },
  created () {
    this.getUserTel();
  },
  methods: {
    getUserTel(){
      this.ruleForm.phone = this.$store.state.user.phone
    },
    goBack() {
      this.$router.go(-1);
    },
    //验证码
    getVCode() {
      let that = this;
      if (/^1[3456789]\d{9}$/.test(that.ruleForm.phone)) {
        let num = 60;
        that.btnIsDisabled = true;
        let timer = setInterval(function() {
          that.VCodeText = num + "秒后可重新发送";
          num -= 1;
          if (num < 0) {
            that.VCodeText = "获取验证码";
            that.btnIsDisabled = false;
            clearInterval(timer);
          }
        }, 1000);
        that.$axios
          .post(
            "/api/captcha",{
              phone: that.ruleForm.phone,
              type: 2
            }
          )
          .then(res => {
            console.log(res);
            if (res.data.code == 200) {
              that.$message({
                message: "验证码发送成功！",
                type: "success"
              });
            } else {
              if (res.data.code == 400) {
                that.$message({
                  message: res.data.message,
                  type: "error"
                });
              } else {
                that.$message({
                  message: "验证码发送失败！",
                  type: "error"
                });
              }
            }
          });
      } else {
        that.$message({
          message: "手机号不能为空且格式不能有误",
          type: "warning"
        });
      }
    },

    //检验旧手机号
    verifyPhone(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .get(
              "api/consumer/verifyPhone?phone=" +
                this.ruleForm.phone.toString() +
                "&code=" +
                this.ruleForm.code.toString()
            )
            .then(res => {
              if (res.data.code == 200) {
                  this.$store.state.isUpdateTel = true; //已经过旧手机验证
                  this.$router.push({path:'/user/set/updateTel'})
              } else {
                this.$toast(res.data.message);
              }
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          this.$toast("请按要求填写手机号码及验证码");
          return false;
        }
      });
    },

  }
};
</script>
<style lang="less">
.user-tel {
  .el-form {
    margin: 0 5%;
    width: 90%;
    .btn-code {
      float: right;
	  color: #FFF;
	  background-color: #409EFF;
	  border-color: #409EFF;
	  border-top-left-radius: 0;
	  border-bottom-left-radius: 0;
    }
  }
}
</style>